/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.panel.layouts;

import java.util.Arrays;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JPanel;
import org.balisunrise.common.FieldComponent;
import org.balisunrise.common.FieldLayout;
import org.balisunrise.common.FieldLayoutGroup;
import org.balisunrise.swing.components.FocusTraversalKeys;
import org.balisunrise.swing.components.panel.LayoutBuilder;

/**
 * Constroi o layout dos componentes de cada campo de uma entidade.<br>
 * O Formato de construção é o mais padrão, sendo o primeiro componente
 * pego do FieldLayout (que deve ser um label) será colocado em uma primeira
 * coluna e os demais componentes serão colocados na segunda coluna.<br>
 * Vale lembrar que no caso do jumpToNextRow não ser usado, os componentes
 * são todos colocados na segunda coluna do FieldLayout anterior.
 *
 * @author Glauco
 */
public class DefaultFieldLayoutBuilder implements LayoutBuilder{

    List<FieldLayout> components;

    javax.swing.GroupLayout layout;

    BaliComponentsGroup grupoDeComponentes;

    BaliComponentsRow row;

    public List<FieldLayout> getComponents() {
        return components;
    }

    public void setComponents(List<FieldLayout> components) {
        this.components = components;
    }

    @Override
    public void buildLayout(JPanel panel) {

        // Cria layout
        layout = new javax.swing.GroupLayout(panel);

        grupoDeComponentes = new BaliComponentsGroup(layout);

        row = null;

        // cria componentes...
        create(components);

        layout.setHorizontalGroup(grupoDeComponentes.getHorizontalGroup(layout));

        layout.setVerticalGroup(grupoDeComponentes.getVerticalGroup(layout));

        panel.setLayout(layout);

        FocusTraversalKeys.setFocusTraversalKeys(panel);
    }

    private void create(List<FieldLayout> components){
        for (FieldLayout fl : components) {
            if(fl instanceof FieldLayoutGroup){
                FieldLayoutGroup group = (FieldLayoutGroup) fl;
                create(Arrays.asList(group.getFieldLayouts()));
            }else
                create(fl);
        }
    }

    private void create(FieldLayout fl){

        if(fl.getFieldComponents().length == 0)
            return;

        // verifica a linha para montar o layout
        if(fl.jumpToNextRow()){

            // Nova linha de componentes
            row = new BaliComponentsRow(layout);

            for (int i = 0; i < fl.getFieldComponents().length; i++) {

                FieldComponent<JComponent> fc = fl.getFieldComponents()[i];

                if (i == 0)
                    row.setFirstColumnComponent(fc.getVisualRepresentation());
                else
                    row.addComponent(fc.getVisualRepresentation());
            }

            // adicioda o row ao grupo
            grupoDeComponentes.addComponentsRow(row);
        } else {
            if(row == null){
                // Nova linha de componentes
                row = new BaliComponentsRow(layout);
                // adicioda o row ao grupo
                grupoDeComponentes.addComponentsRow(row);
            }

            for (int i = 0; i < fl.getFieldComponents().length; i++) {

                FieldComponent<JComponent> fc = fl.getFieldComponents()[i];

                // Adiciona o Componente
                row.addComponent(fc.getVisualRepresentation());
            }
        }
    }
}
